package com.zhny.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mysql.cj.util.StringUtils;
import com.zhny.dao.KnowledgeMapper;
import com.zhny.entity.QueryPageBean;
import com.zhny.pojo.Knowledge;
import com.zhny.service.KnowledgeService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class KnowledgeServiceImpl implements KnowledgeService {

    @Autowired
   private KnowledgeMapper knowledgeMapper;


    /**
     * 新增
     * @param knowledge
     */
    @Override
    public void add(Knowledge knowledge) {
        knowledgeMapper.insert(knowledge);
    }

    /**
     * 动态修改
     * @param knowledge
     */
    @Override
    public void update(Knowledge knowledge) {
        knowledgeMapper.updateByPrimaryKeySelective(knowledge);
    }

    /**
     * 批量删除
     * @param id
     */
    @Override
    public void delete(List<Integer> id) {
        for (Integer ids : id) {
            knowledgeMapper.deleteByPrimaryKey(ids);
        }
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public Page<Knowledge> findPage(QueryPageBean queryPageBean) {
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage, pageSize);

        Page<Knowledge> page=null;
        if (!StringUtils.isNullOrEmpty(queryString)) {
            //page = knowledgeMapper.findLikeQuary("%" + queryString + "%");
            Example example = new Example(Knowledge.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.orLike("cropname","%" + queryString + "%");
            criteria.orLike("pestname","%" + queryString + "%");
            criteria.orLike("weedname","%" + queryString + "%" );
             page= (Page<Knowledge>) knowledgeMapper.selectByExample(example);

        }
        return  page;
        //Page<Knowledge> page= (Page<Knowledge>) knowledgeMapper.selectAll();
        //return  page;
    }


}
